package com.vanke.kotlin_mvp.statistics

import android.os.Bundle
import android.support.v4.app.NavUtils
import android.support.v4.view.GravityCompat
import android.support.v4.widget.DrawerLayout
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import com.vanke.kotlin_mvp.R
import com.vanke.kotlin_mvp.data.Injection
import com.vanke.kotlin_mvp.util.ActivityUtils
import kotlinx.android.synthetic.main.activity_statistics.*

class StatisticsActivity : AppCompatActivity() {

    private lateinit var drawerLayout: DrawerLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_statistics)

        setSupportActionBar(toolbar)

        supportActionBar?.let {
            it.setTitle("statistics")
            it.setHomeAsUpIndicator(R.drawable.ic_menu)
            it.setDisplayHomeAsUpEnabled(true)
        }

        drawerLayout = draw_layout.apply {
            setStatusBarBackground(R.color.colorPrimaryDark)
        }

        nav_view.setNavigationItemSelectedListener { item: MenuItem ->
            if (item.itemId == R.id.list_navigation_menu_item) {
                NavUtils.navigateUpFromSameTask(this@StatisticsActivity)
            }
            item.isChecked = true
            drawerLayout.closeDrawers()
            true
        }

        val statisticsFragment = (supportFragmentManager.findFragmentById(R.id.contentFrame) as StatisticsFragment?
                ?: StatisticsFragment.newInstance().also {
            ActivityUtils.addFragmentToActivity(supportFragmentManager, it, R.id.contentFrame)
        })

        StatisticsPresenter(Injection.provideTasksRepository(applicationContext),statisticsFragment)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home){
            drawerLayout.openDrawer(GravityCompat.START)
            return true
        }
        return super.onOptionsItemSelected(item)
    }
}
